import 'package:flutter/material.dart';
import 'chat/messagePage.dart';
import 'contacts/contacts.dart';
import 'personal/personal.dart';

class AppHomePage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return AppHomePageState();
  }
}

class AppHomePageState extends State<AppHomePage> {


  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(

      appBar: AppBar(

        title: Text('message模块'),
        centerTitle: true,

        ///title 右边的按钮
        actions: <Widget>[

          GestureDetector(

            onTap: () {},

            child: Icon(Icons.search),
          ),


          Padding(

            padding: const EdgeInsets.only(left: 10.0, right: 10.0),

            child: GestureDetector(

              onTap: () {

                ///为什么可以调用showMenu方法，在按钮中也可以调用
                showMenu(

                    context: context,
                    //定位在页面的右上角
                    position: RelativeRect.fromLTRB(500.0, 76.0, 0.0, 0.0),
                    items: <PopupMenuEntry>[

                      _popupMeuItem(
                          '发起会话', imagePath: 'images/icon_menu_group.png'),
                      _popupMeuItem(
                          '添加好友', imagePath: 'images/icon_menu_addfriend.png'),
                      _popupMeuItem('联系客服', iconData: Icons.person),
                    ]
                );
              },

              //菜单按钮
              child: Icon(Icons.add),
            ),
          )

        ],
      ),


      bottomNavigationBar: BottomNavigationBar(

          type: BottomNavigationBarType.fixed,

          currentIndex: _currentPageIndex,

          onTap: (index) {
            setState(() {
              _currentPageIndex = index;
            });
          },

          items: <BottomNavigationBarItem>[

            BottomNavigationBarItem(

                title: Text('聊天', style: TextStyle(
                    color: _currentPageIndex == 0 ? Color(0xFF46c01b) : Color(
                        0xff999999)),),

                icon: _currentPageIndex == 0 ?
                Image.asset(
                  'images/message_pressed.png', width: 32.0, height: 28.0,) :
                Image.asset(
                  'images/message_normal.png', width: 32.0, height: 28.0,)),

            BottomNavigationBarItem(

                title: Text('好友', style: TextStyle(
                    color: _currentPageIndex == 1 ? Color(0xFF46c01b) : Color(
                        0xff999999)),),

                icon: _currentPageIndex == 1 ?
                Image.asset(
                  'images/contact_list_pressed.png', width: 32.0, height: 28.0,) :
                Image.asset(
                  'images/contact_list_normal.png', width: 32.0, height: 28.0,)),


            BottomNavigationBarItem(

                title: Text('我的', style: TextStyle(
                    color: _currentPageIndex == 2 ? Color(0xFF46c01b) : Color(
                        0xff999999)),),

                icon: _currentPageIndex == 2 ?
                Image.asset(
                  'images/profile_pressed.png', width: 32.0, height: 28.0,) :
                Image.asset(
                  'images/profile_normal.png', width: 32.0, height: 28.0,)),


          ]

      ),

      body: currentPage(),


    );
  }


  int _currentPageIndex = 0;

  //聊天页面
  MessagePage messagePage;

  //好友页面
  ContactsPage contactsPage;

  //个人页面
  PersonalPage personalPage;

  //获取当前页面
  currentPage() {
    switch (_currentPageIndex) {
      case 0:
        if (messagePage == null) {
          messagePage = MessagePage();
        }

        return messagePage;

      case 1:
        if (contactsPage == null) {
          contactsPage = ContactsPage();
        }

        return contactsPage;

      case 2:
        if (personalPage == null) {
          personalPage = PersonalPage();
        }
    }
  }

  /// 创建右上角meu item
  _popupMeuItem(String title, {String imagePath, IconData iconData}) {
    return PopupMenuItem(

      child: Row(

        children: <Widget>[

          //判断使用图片路径还是图标
          imagePath != null
              ? Image.asset(imagePath, width: 32.0, height: 32.0)
              : SizedBox(width: 32.0,
            height: 32.0,
            child: Icon(iconData, color: Colors.white,),),

          //菜单内容
          Container(

            padding: const EdgeInsets.only(left: 20.0),

            child: Text(title, style: TextStyle(color: Colors.black),),
          )
        ],
      ),
    );
  }


}